package com.utn.ab.demo.controller;


import com.fasterxml.jackson.annotation.JsonView;
import com.utn.ab.demo.dto.User;
import com.utn.ab.demo.entity.UserQueryCondition;
import com.utn.ab.demo.mapper.DemoUserMapper;
import com.utn.ab.demo.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @创建人 liuchunlin
 * @创建时间 15:43
 * @描述
 */
@RestController
@Slf4j
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private DemoUserMapper demoUserMapper;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private UserRepository userRepository;

    @RequestMapping(value = "/count")
    public int count(){
//        log.info("count");
        RBucket<Object> keyObj = redissonClient.getBucket("count");
        Integer count = (Integer) keyObj.get();
        if(count == null){
            count = demoUserMapper.testCountBySex(1);
            keyObj.set(count);
        }
        return count;
    }

//    @RequestMapping(value = "/save")
//    public void save(){
//        User user = new User();
//        user.setId(2);
//        user.setName("utn");
//        user.setBirth(1231212);
//        user.setSex(1);
//        userRepository.save(user);
//    }
//
//    @RequestMapping(value = "/delete")
//    public void delete(){
//        userMapper.testDeleteBySex("utn",1);
//    }
//
//    @RequestMapping(value = "/select")
//    public List<User> select(){
//        return userMapper.testSelect("utn",1);
//    }
//
//    @RequestMapping(value = "/find")
//    public List<User> find(){
//        List<User> users = userRepository.findAll();
//        return users;
//    }


    @GetMapping
    @JsonView(User.UserSimpleView.class)
    //@ApiOperation(value = "用户查询服务")
    //@RequestMapping("/query")
    public List<User> query(UserQueryCondition condition,
                            @PageableDefault(page = 2, size = 17, sort = "username,asc") Pageable pageable) {

        System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE));

        System.out.println(pageable.getPageSize());
        System.out.println(pageable.getPageNumber());
        System.out.println(pageable.getSort());

        List<User> users = new ArrayList<>();
        users.add(new User());
        users.add(new User());
        users.add(new User());
        return users;
    }

    @GetMapping("/{id:\\d+}")
    //@JsonView(User.UserDetailView.class)
    public User getInfo(//@ApiParam("用户id")
                            @PathVariable String id) {
//		throw new RuntimeException("user not exist");
        System.out.println("进入getInfo服务");
        User user = new User();
        user.setUsername("tom");
        return user;
    }

    @PostMapping
    //@ApiOperation(value = "创建用户")
    public User create(@Valid @RequestBody User user) {

        System.out.println(user.getId());
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }

    @PutMapping("/{id:\\d+}")
    public User update(@Valid @RequestBody User user, BindingResult errors) {

        System.out.println(user.getId());
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }

    @DeleteMapping("/{id:\\d+}")
    public void delete(@PathVariable String id) {
        System.out.println(id);
    }

}
